<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Style\Controller;

use Al\Core\Lib\Controller,
    Al\Style\Lib\Breadcrumbs,
    Al\Core\Lib\Service,
    Al\Style\Lib\Form\Element\Choice;

/**
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
class MainController extends Controller {

  /**
   * Renders a less template.
   */
  public function lessAction($theme) {
    //make sure that the plugins' public directory is publicly accessible
    Service::get('config')->getPluginWebRoot('Al\Style');
    return $this->render($this->getConfig("theme.all.$theme"));
  }

  /**
   * Transforms the translation texts configured with yaml into javascript to be
   * used on the client side.
   */
  public function jslangAction($_locale) {
    return $this->render($this->getConfig('lang'), array('locale' => $_locale));
  }

  /**
   * Renders core javascript classes.
   */
  public function jsAction($filename) {
    return $this->render($this->getConfig('scripts') . $filename);
  }

  /**
   * Renders a theme selector combo box.
   */
  public function themeSelectorAction() {
    $options = array();
    foreach ($this->getConfig('theme.all') as $key => $value) {
      $options[Service::get('request')->buildRoutePath('bootstrap_less', array(
        'theme' => $key))] = $key;
    }
    return $this->renderText(new Choice(array(
      'options' => $options,
      'value' => $this->getConfig('theme.current'),
      'attr' => array(
        'style' => 'width:200px',
        'onchange' => 'Al.changeTheme($(this).val())'
      ),
    )));
  }

  /**
   * Renders the main menu.
   */
  public function mainMenuAction() {
    return $this->render($this->getConfig('main_menu'));
  }

  /**
   * Renders the breadcrumbs.
   */
  public function breadcrumbsAction() {
    return $this->renderText(new Breadcrumbs);
  }

  /**
   * Renders the plupload localization.
   */
  public function pluploadLocaleAction() {
    $locale = Service::get('http.session._locale', 'en');
    return $this->render($this->getConfig('plupload.locale'), array(
      'file' => $this->getConfig("plupload.i18n.$locale"),
    ));
  }

  /**
   * Uploads files to a temporary directory.
   */
  public function pluploadUploadAction() {
    $resp = array('success' => true);
    try {
      foreach (glob(($path = __DIR__ . '/../Resources/tmp/') . '*') as $dir) {
        if (filemtime($dir) < time() - 86400) {
          foreach(glob("$dir/*") as $file) unlink($file);
          @rmdir($dir);
        }
      }
      Service::get('http.files')->move($path .
        Service::get('http.post.form_id'), 'file');
    } catch (\Exception $e) {
      $resp['success'] = false;
      $resp['error'] = $e->getMessage();
    }
    return $this->renderJson($resp);
  }

}